component{
	include "../../inc/lib.cfm"; 

	public any function list(rc){
		
		var result = {};
		var offset = (rc.page-1)*rc.rows;
		hql = "from ads where " & rc.sqlWhere & rc.sqlSort;
		var result["rows"] = ormExecuteQuery(hql, false, {maxResults=rc.rows, offset=offset});
		var totalhql = "select count(id) as total from ads where " & rc.sqlWhere ;
		var result["total"] = ormExecuteQuery(totalhql, true);
		return result;
	}
	
	
	public any function category(rc){
		
		var result = {};
		var hql = " from categoryflat where categorytypeid=#rc.categorytypeid# order by categoryid asc,seq asc";
		var result["rows"] = ormExecuteQuery(hql,false);
		var totalhql = "select count(id) as total from categoryflat where categorytypeid=#rc.categorytypeid#";
		var result["total"] = ormExecuteQuery(totalhql,true);
		
		newResult = [];
		
		for(var i=1;i<=arraylen(result.rows);i++){
			var tmpResult = {};
			adstotalhql = "select count(id) as total from ads where categoryid=#result.rows[i].getId()#";
			tmpResult['rcount'] = ormExecuteQuery(adstotalhql,true); 
			
			tmpResult["text"] = result.rows[i].getText();
			tmpResult["id"] = result.rows[i].getId();
			tmpResult["description"] = result.rows[i].getDescription();
			tmpResult["seq"] = result.rows[i].getSeq();
			tmpResult["_parentId"] = result.rows[i].getCategoryid();
			newResult[i] = tmpResult;
		}
		result["rows"] = newResult;
		return result;
	}

	public any function save(any rc){
		transaction {
            var q = entityNew("ads");
			include "../../inc/saveandupdate.cfm";
        }
        return q;
	}
	
	public any function delete(numeric id){
		var q = entityLoadByPk("ads", arguments.id);
		entityDelete(q);
		transactionCommit();
		return q;
	}
	
	public any function update(any rc){
		transaction {
            var q = entityLoadByPk("ads", rc.id);
			include "../../inc/saveandupdate.cfm";
        }
        return q;
	}
	
	public any function get(numeric id){
		result = entityLoadByPk("ads", arguments.id);
		return result;
	}
	
	//发布操作
	public any function publish(numeric id){
		q = entityLoadByPk("ads", arguments.id);
		if( q.getispublish()){v = 0;}else{v = 1;}
		q.setispublish(v);
		entitySave(q);
		transactionCommit();
		return v;
	}
	
	public any function publishbat(rc){
		result = ORMExecuteQuery("update ads set ispublish=:ispublish WHERE id in (#rc.ids#)",{ispublish = lsparsenumber(rc.value)});
		return result;
	}
	
}